<?php

namespace plugin\admin\app\controller\card;

use plugin\admin\app\exception\AdminException;
use plugin\admin\app\logic\card\CardLogic;
use support\Request;
use support\Response;
use plugin\admin\app\controller\AuthController;

/**
 * Banner管理
 */
class IndexController extends AuthController
{

    /**
     * 浏览
     * @return Response
     * @throws \Throwable
     */
    public function index(): Response
    {
        $data = CardLogic::getInstance()->getConfig();

        return raw_view('card/index', [
            'avatars' => join("\n", json_decode($data['avatars'], 1)),
            'nicknames' => join("\n", json_decode($data['nicknames'], 1)),
            'signs' => join("\n", json_decode($data['signs'], 1)),
        ]);
    }


    /**
     * 保存配置
     * @param Request $request
     * @return Response
     * @throws AdminException
     */
    public function save(Request $request): Response
    {
        if ($request->method() != 'POST') {
            throw new AdminException(4020, '请求错误');
        }
        return CardLogic::getInstance()->save($request);
    }

}
